package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author Administrator
 */
public class Demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        System.out.println("请输入昵称:");
        String nick = scanner.nextLine();
        try (
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "select username from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("该用户已存在,请重新输入用户名!");
                return;
            }
            String sql1="insert into user values (null,?,?,?)";
            PreparedStatement insertPs = conn.prepareStatement(sql1);
            insertPs.setString(1,username);
            insertPs.setString(2,password);
            insertPs.setString(3,nick);
            int row = insertPs.executeUpdate();
            System.out.println(row+"个用户注册成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
